<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript">

        function buttonLY() {
    var ursenameObj=document.getElementById("ursename");
    var ursenameText = ursenameObj.value;

    var patt = /^\w{5,12}$/;

    var ursenamespanObj=document.getElementById("ursenamespan");
    // var span2Obj=document.getElementsByName("span2");
    var span2Obj=document.getElementById("span2");

    if (patt.test(ursenameText)){
        ursenamespanObj.innerHTML="<img src=\"正确.png\" width=\"20\"height=\"20\">用户名正确";
        // ursenamespanObj.innerHTML="用户名正确";

    }else {
        ursenamespanObj.innerHTML="<img src=\"错误.png\" width=\"20\"height=\"20\">用户名不正确";
        // ursenamespanObj.innerHTML="用户名不正确";
    }

//     if (patt.test(ursenameText)){
//         ursenamespanObj.innerHTML="用户名正确";
//
//
//     }else {
//         ursenamespanObj.innerHTML="用户名不正确";
//     }
}
    </script>
</head>
<body>

用户名：<input type="text"id="ursename">

<span style="color: dodgerblue"id="ursenamespan"></span>

<span style="color: dodgerblue"id="span2"></span>

<button onclick="buttonLY()">确认</button>

</body>
</html>